import 'package:flutter/material.dart';
import 'package:flutter_app/ui/pages/collection.dart';
import 'package:transparent_image/transparent_image.dart';
import 'package:flutter_app/api/api.dart' as api;

class MySelfPage extends StatefulWidget {
  @override
  MySelfPageState createState() {
    return new MySelfPageState();
  }

  const MySelfPage();
}

class MySelfPageState extends State<MySelfPage> {
  String _imgUrl =
      "http://www.wanandroid.com/blogimgs/ac15e9b4-2d17-4a30-895b-c31522f7e823.jpg";
  Map<String, dynamic> _data;
  bool _login = false;
  List list;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: new ListView(children: _getBody()),
    );
  }

  @override
  void dispose() {
    super.dispose();
    api.clearOb();
  }

  _getBody() {
    var view = <Widget>[];
    view.add(Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        Container(
          margin: const EdgeInsets.all(20.0),
          width: 100.0,
          height: 100.0,
          child: new ClipOval(
            child: new FadeInImage.memoryNetwork(
              placeholder: kTransparentImage,
              //预览图
              fit: BoxFit.fill,
              image: _imgUrl,
            ),
          ),
        ),
      ],
    ));
    view.add(
      new GestureDetector(
        child: Container(
          margin: const EdgeInsets.all(20.0),
          padding: const EdgeInsets.all(10.0),
          alignment: Alignment.center,
          child: new Text(_data == null ? "未登录" : _data['username']),
          decoration: new BoxDecoration(
            borderRadius: const BorderRadius.all(
              const Radius.circular(3.0),
            ),
            color: Colors.tealAccent,
          ),
        ),
        onTap: () {
          Navigator.of(context).pushNamed("login").then((obj) {
            setState(() {
              if (obj != null) {
                _data = obj;
                _login = true;
              }
            });
          });
        },
      ),
    );
    if (_login) {
      view.add(
        new GestureDetector(
          child: new Container(
            margin: const EdgeInsets.only(left: 20.0, right: 20.0, top: 20.0),
            padding: const EdgeInsets.all(13.0),
            alignment: Alignment.centerLeft,
            child: new Text("我的收藏"),
            color: new Color(0xFFF4F4F4),
          ),
          onTap: () {
            Navigator.of(context).push(new MaterialPageRoute(builder: (ctx) {
              return const CollectionPage();
            }));
          },
        ),
      );
      view.add(
        new GestureDetector(
          child: new Container(
            margin: const EdgeInsets.only(left: 20.0, right: 20.0),
            padding: const EdgeInsets.all(13.0),
            alignment: Alignment.centerLeft,
            child: new Text("关于我们"),
            color: new Color(0xFFF4F4F4),
          ),
          onTap: () {
            print("onTap");
          },
        ),
      );
      view.add(
        new GestureDetector(
          child: new Container(
            margin: const EdgeInsets.only(left: 20.0, right: 20.0),
            padding: const EdgeInsets.all(13.0),
            alignment: Alignment.centerLeft,
            child: new Text("退出登录"),
            color: new Color(0xFFF4F4F4),
          ),
          onTap: () {
            setState(() {
              _login = false;
              _data = null;
            });
          },
        ),
      );
    }
    return view;
  }
}
